package com.zjjt.dc.modules.srv.controller.lw;

import com.zjjt.dc.common.query.QueryGenerator;
import com.zjjt.dc.modules.datacatch.entity.lw.DcCatchDataLwPriceDr;
import com.zjjt.dc.security.util.JwtTokenUtil;
import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.*;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.List;
import java.util.Arrays;
import com.zjjt.dc.modules.srv.entity.DcCatchDataLwPriceCheck;
import com.zjjt.dc.modules.srv.service.lw.IDcCatchDataLwPriceCheckService;
import com.zjjt.dc.common.api.CommonResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@Api(tags = "DcCatchDataLwPriceCheck 控制器")
@RestController
@RequestMapping("/api/v1/lw/price_check")
public class DcCatchDataLwPriceCheckController {

    @Autowired
    private IDcCatchDataLwPriceCheckService dcCatchDataLwPriceCheckService;

    

    @ApiOperation("分页查询")
    @GetMapping("/pagelist")
    public CommonResult<IPage<DcCatchDataLwPriceCheck>> pageQuery(DcCatchDataLwPriceCheck entity, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                          HttpServletRequest req) {
        // ...分页查询实现...
        QueryWrapper<DcCatchDataLwPriceCheck> queryWrapper = QueryGenerator.initQueryWrapper(entity, req.getParameterMap());
        queryWrapper.orderByDesc("create_time");
        Page<DcCatchDataLwPriceCheck> page = new Page<DcCatchDataLwPriceCheck>(pageNo, pageSize);
        IPage<DcCatchDataLwPriceCheck> pageList = dcCatchDataLwPriceCheckService.page(page, queryWrapper);
        return CommonResult.success(pageList);
    }
    @ApiOperation("关键字查询")
    @GetMapping("/search")
    public List<DcCatchDataLwPriceCheck> keywordQuery(String keyword) {
        // ...关键字查询实现...
        return null;
    }
    @ApiOperation("新增")
    @PostMapping("/add")
    public CommonResult<?> add(@RequestBody DcCatchDataLwPriceCheck entity) {
        dcCatchDataLwPriceCheckService.save(entity);
        return CommonResult.success("添加成功！");
    }
    @ApiOperation("批量新增")
    @PostMapping("/addBatch")
    public CommonResult<?> add(@RequestBody List<DcCatchDataLwPriceCheck> entitys) {
        dcCatchDataLwPriceCheckService.saveBatch(entitys);
        return CommonResult.success("添加成功！");
    }
    @ApiOperation("修改")
    @PutMapping("/update")
    public CommonResult<?> update(@RequestBody DcCatchDataLwPriceCheck entity) {
        dcCatchDataLwPriceCheckService.updateById(entity);
        return CommonResult.success("更新成功！");
    }
    @ApiOperation("批量修改")
    @PutMapping("/updateBatch")
    public CommonResult<?> updateBatch(@RequestBody List<DcCatchDataLwPriceCheck> entitys) {
        dcCatchDataLwPriceCheckService.updateBatchById(entitys);
        return CommonResult.success("更新成功！");
    }
    @ApiOperation("删除")
    @DeleteMapping("/delete/{id}")
    public CommonResult<?> delete(@PathVariable String id) {
        dcCatchDataLwPriceCheckService.removeById(id);
        return CommonResult.success("删除成功!");
    }
    @DeleteMapping(value = "/deleteBatch")
    @ApiOperation(value = "批量删除DcCatchDataLwPriceCheck", notes = "批量删除DcCatchDataLwPriceCheck")
    public CommonResult<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        dcCatchDataLwPriceCheckService.removeByIds(Arrays.asList(ids.split(",")));
        return CommonResult.success("批量删除成功！");
    }
    @GetMapping(value = "/queryById")
    @ApiOperation(value = "通过ID查询DcCatchDataLwPriceCheck", notes = "通过ID查询DcCatchDataLwPriceCheck")
    public CommonResult<?> queryById(@ApiParam(name = "id", value = "示例id", required = true) @RequestParam(name = "id", required = true) String id) {
        DcCatchDataLwPriceCheck result = dcCatchDataLwPriceCheckService.getById(id);
        return CommonResult.success(result);
    }
    @GetMapping(value = "/queryByCreator")
    @ApiOperation(value = "通过ID查询DcCatchDataLwPriceCheck", notes = "通过Creator查询DcCatchDataLwPriceCheck")
    public CommonResult<?> queryByCreator(@ApiParam(name = "user_id", value = "用户id", required = false) @RequestParam(name = "id", required = false) String id) {
        if(id == null) id = JwtTokenUtil.getCurrentUserId();
        List<DcCatchDataLwPriceCheck> result = dcCatchDataLwPriceCheckService.getCreateById(id);
        return CommonResult.success(result);
    }
    //导入execl
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public CommonResult<?> importExcel(@RequestParam("file") MultipartFile file) {
        ImportParams params = new ImportParams();
        params.setTitleRows(0);
        params.setHeadRows(1);
        params.setNeedSave(true);
        try {
            List<DcCatchDataLwPriceCheck> list = ExcelImportUtil.importExcel(file.getInputStream(), DcCatchDataLwPriceCheck.class, params);

            return CommonResult.success(dcCatchDataLwPriceCheckService.getDcCatchDataLwPriceVO(list));
        } catch (Exception e) {

        }finally {
            try {
                file.getInputStream().close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        return CommonResult.success();
    }

}